package com.aio.news.view;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aidi.lib.TAInjectView;
import com.aio.news.AioApplication;
import com.aio.news.R;

public class DetailMoreView extends ViewGroup implements View.OnClickListener {

	private boolean topMode = true;
	private boolean isOpen = false;
	private View view;
	private View backImg;

	@TAInjectView(id = R.id.tab_top)
	private TabStateImageView topModeView;
	@TAInjectView(id = R.id.tab_bottom)
	private TabStateImageView bottomModeView;
	@TAInjectView(id = R.id.mode_day)
	private TabStateImageView dayMode;
	@TAInjectView(id = R.id.mode_night)
	private TabStateImageView nightMode;
	@TAInjectView(id = R.id.small)
	private TabStateImageView sizeSmall;
	@TAInjectView(id = R.id.middle)
	private TabStateImageView sizeMiddle;
	@TAInjectView(id = R.id.big)
	private TabStateImageView sizeLarge;

	public DetailMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public DetailMoreView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public DetailMoreView(Context context) {
		super(context);
		init();
	}
	
	public static interface MoreWindowState{
		void close();
	}

	void init() {
		setOnClickListener(backOnClick);
		backImg = new View(getContext());
		ViewGroup.LayoutParams backParams = new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);
		backImg.setBackgroundColor(0xff000000);
		addView(backImg, backParams);
		view = View.inflate(getContext(), R.layout.detail_more_item, null);
		AioApplication.getInjector().injectView(view, this);
		view.setOnClickListener(backOnClick);
		topModeView.setOnClickListener(this);
		bottomModeView.setOnClickListener(this);
		dayMode.setOnClickListener(this);
		nightMode.setOnClickListener(this);
		sizeSmall.setOnClickListener(this);
		sizeMiddle.setOnClickListener(this);
		sizeLarge.setOnClickListener(this);
		TextView mText = (TextView) view.findViewById(R.id.text);
		mText.setText(R.string.detail_more_position);
		addView(view);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		for (int i = 0; i < getChildCount(); i++) {
			View child = getChildAt(i);
			measureChild(child, widthMeasureSpec, heightMeasureSpec);
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		for (int i = 0; i < getChildCount(); i++) {
			View child = getChildAt(i);
			if (child.getId() == R.id.more_item) {
				if (topMode) {
					child.layout(l, t, child.getMeasuredWidth(),
							child.getMeasuredHeight());
				} else {
					child.layout(l,
							getMeasuredHeight() - child.getMeasuredHeight(), r,
							getMeasuredHeight());
				}
			} else {
				child.layout(l, t, r, b);
			}
		}
	}

	public void openOrClose() {
		alphaAnim();
		if (isOpen) {
			isOpen = false;
			closeAnim();
		} else {
			isOpen = true;
			showAnim();
		}
	}

	public void showAnim() {
		openAnimation(view, topMode);
	}

	public void closeAnim() {
		closeAnimation(view, topMode);
	}

	public void openAnimation(View view, boolean topMode) {
		setVisibility(View.VISIBLE);
		ObjectAnimator anim = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y,
				topMode ? -getMeasuredHeight() : getMeasuredHeight(), 0.0f);
		anim.setDuration(300);
		anim.start();
	}

	public void closeAnimation(View view, boolean topMode) {
		ObjectAnimator anim = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y,
				0, topMode ? -view.getMeasuredHeight() : getMeasuredHeight());
		anim.setDuration(300);
		anim.start();
		anim.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {
			}

			@Override
			public void onAnimationRepeat(Animator animation) {
			}

			@Override
			public void onAnimationEnd(Animator animation) {
				DetailMoreView.this.setVisibility(View.INVISIBLE);
			}

			@Override
			public void onAnimationCancel(Animator animation) {
			}
		});
	}

	private void alphaAnim() {
		ObjectAnimator anim = ObjectAnimator.ofFloat(backImg, View.ALPHA,
				isOpen ? 0.6f : 0.0f, isOpen ? 0.0f : 0.6f);
		anim.setDuration(300);
		anim.start();
	}

	View.OnClickListener backOnClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v.getId() != R.id.more_item) {
				openOrClose();
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tab_top:
			topModeView.setSelected(true);
			bottomModeView.setSelected(false);
			break;
		case R.id.tab_bottom:
			topModeView.setSelected(false);
			bottomModeView.setSelected(true);
			break;
		case R.id.mode_day:
			dayMode.setSelected(true);
			nightMode.setSelected(false);
			break;
		case R.id.mode_night:
			dayMode.setSelected(false);
			nightMode.setSelected(true);
			break;
		case R.id.small:
			sizeSmall.setSelected(true);
			sizeMiddle.setSelected(false);
			sizeLarge.setSelected(false);
			break;
		case R.id.middle:
			sizeSmall.setSelected(false);
			sizeMiddle.setSelected(true);
			sizeLarge.setSelected(false);
			break;
		case R.id.big:
			sizeSmall.setSelected(false);
			sizeMiddle.setSelected(false);
			sizeLarge.setSelected(true);
			break;

		default:
			break;
		}
	}

}
